package net.zjitc.controller.admin;

import net.zjitc.common.ResponseMessage;
import net.zjitc.common.ServiceResponse;
import net.zjitc.pojo.Notice;
import net.zjitc.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @AUTHOR zhj
 * @DATE 2019/3/28 14:48
 */
@Controller
@RequestMapping("/manage/notice")
public class NoticeController {
    @Autowired
    NoticeService noticeService;


    @RequestMapping("/findAll")
    public String findAll( Model model){
        List<Notice> list = noticeService.findAll();
        model.addAttribute("list",list);
        return "/admin/notices/list.jsp";
    }


    @RequestMapping("/findById")
    public String findById(int id,Model model){
        Notice notice = noticeService.findById(id);
        model.addAttribute("notice",notice);
        return "/admin/notices/edit.jsp";
    }


    //ajax请求：响应数据json（提示信息--》重定向到findALL.do）

    /**
     * 1. 修改的状态：成功 ： 修改成功
     *                失败 ： 修改失败
     *                参数异常：
     * @param notice
     * @param model
     * @return
     */
    @RequestMapping("/editNotice")
    @ResponseBody
    public ResponseMessage editNotice(@RequestBody Notice notice,Model model){
        int resultCount = noticeService.updateNotice(notice);

        //响应成功
        if (resultCount>0){
            return new ResponseMessage(0,"修改成功");
        }

        return new ResponseMessage(1,"修改失败");
    }


    @RequestMapping("/editNoticeNew")
    @ResponseBody
    public ServiceResponse editNoticeNew(Notice notice, Model model){
        ServiceResponse serviceResponse = noticeService.editNotice(notice);
        return serviceResponse;
    }

}
